package com.tulingxueyuan.mall.modules.oms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tulingxueyuan.mall.modules.oms.model.OmsOrder;
import com.tulingxueyuan.mall.modules.oms.model.OmsOrderReturnApply;
import com.tulingxueyuan.mall.modules.oms.mapper.OmsOrderReturnApplyMapper;
import com.tulingxueyuan.mall.modules.oms.model.dto.OmsOrderReturnApplyExampleDTO;
import com.tulingxueyuan.mall.modules.oms.model.dto.OmsOrderReturnApplyResultDTO;
import com.tulingxueyuan.mall.modules.oms.model.dto.OmsReturnApplyQueryParamDTO;
import com.tulingxueyuan.mall.modules.oms.model.dto.OmsUpdateStatusParamDTO;
import com.tulingxueyuan.mall.modules.oms.service.OmsOrderReturnApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.querydsl.QuerydslRepositoryInvokerAdapter;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单退货申请 服务实现类
 * </p>
 *
 * @author zhaofuhao
 * @since 2022-03-03
 */
@Service
public class OmsOrderReturnApplyServiceImpl extends ServiceImpl<OmsOrderReturnApplyMapper, OmsOrderReturnApply> implements OmsOrderReturnApplyService {
    @Autowired
    private OmsOrderReturnApplyMapper returnApplyDao;

    @Override
    public Page list(OmsReturnApplyQueryParamDTO queryParam) {
        Page page = new Page(queryParam.getPageNum(),queryParam.getPageSize());
        QueryWrapper<OmsOrderReturnApply> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<OmsOrderReturnApply> lambdaWrapper= queryWrapper.lambda();
        //服务单号
        if (queryParam.getId()!=null&&queryParam.getId()>0){
            lambdaWrapper.eq(OmsOrderReturnApply::getId,queryParam.getId());
        }
        //处理状态
        if (queryParam.getStatus()!=null){
            lambdaWrapper.eq(OmsOrderReturnApply::getStatus,queryParam.getStatus());

        }
        //申请时间
        if (!StrUtil.isBlank(queryParam.getCreateTime())){
            lambdaWrapper.like(OmsOrderReturnApply::getCreateTime,queryParam.getCreateTime());
        }
        //操作人员
        if (!StrUtil.isBlank(queryParam.getHandleMan())){
            lambdaWrapper.eq(OmsOrderReturnApply::getHandleMan, queryParam.getHandleMan());
        }
        //处理时间
        if (!StrUtil.isBlank(queryParam.getHandleTime())){
            lambdaWrapper.like(OmsOrderReturnApply::getHandleTime,queryParam.getHandleTime());
        }
        return this.page(page,lambdaWrapper);
    }

    @Override
    public OmsOrderReturnApplyResultDTO getItem(Long id) {
        return returnApplyDao.getDetail(id);
    }


    @Override
    public boolean updateStatus(Long id, OmsUpdateStatusParamDTO statusParam) {
        Integer status = statusParam.getStatus();
        OmsOrderReturnApply returnApply = new OmsOrderReturnApply();
        if(status.equals(1)){
            //确认退货
            returnApply.setId(id);
            returnApply.setStatus(1);
            returnApply.setReturnAmount(statusParam.getReturnAmount());
            returnApply.setCompanyAddressId(statusParam.getCompanyAddressId());
            returnApply.setHandleTime(new Date());
            returnApply.setHandleMan(statusParam.getHandleMan());
            returnApply.setHandleNote(statusParam.getHandleNote());
        }else if(status.equals(2)){
            //完成退货
            returnApply.setId(id);
            returnApply.setStatus(2);
            returnApply.setReceiveTime(new Date());
            returnApply.setReceiveMan(statusParam.getReceiveMan());
            returnApply.setReceiveNote(statusParam.getReceiveNote());
        }else if(status.equals(3)){
            //拒绝退货
            returnApply.setId(id);
            returnApply.setStatus(3);
            returnApply.setHandleTime(new Date());
            returnApply.setHandleMan(statusParam.getHandleMan());
            returnApply.setHandleNote(statusParam.getHandleNote());
        }else{
            return false;
        }
        return returnApplyDao.updateByPrimaryKeySelective(returnApply);
    }

    @Override
    public boolean delete(List<Long> ids) {
        OmsOrderReturnApplyExampleDTO example = new OmsOrderReturnApplyExampleDTO();
        example.createCriteria().andIdIn(ids).andStatusEqualTo(3);
        return returnApplyDao.deleteByExample(example);
    }
}
